package com.cskaoyan.market.admin.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.cskaoyan.market.admin.service.MarketAdminBrandService;
import com.cskaoyan.market.admin.service.impl.MarketAdminBrandServiceImpl;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

/**
 * 品牌制造商相关接口
 *
 * @author BlackFlame33
 * @date 2023/03/19
 **/
@WebServlet("/admin/brand/*")
public class AdminBrandController extends HttpServlet {
    private MarketAdminBrandService marketAdminBrandService = new MarketAdminBrandServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/brand", "");
        switch (op) {
            case "/list":
                getBrandList(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void getBrandList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        List<MarketBrand> list = marketAdminBrandService.getBrandList(page, limit, sort, order, id, name);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/brand", "");
        switch (op) {
            case "/create":
                createBrand(req, resp);
                break;
            case "/update":
                updateBrand(req, resp);
                break;
            case "/delete":
                deleteBrand(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void deleteBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        marketAdminBrandService.deleteBrand(id);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void updateBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);

        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String desc = JacksonUtil.parseString(requestBody, "desc");
        Integer floorPrice = JacksonUtil.parseInteger(requestBody, "floorPrice");
        String name = JacksonUtil.parseString(requestBody, "name");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        // Integer sortOrder = JacksonUtil.parseInteger(requestBody, "sortOrder");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        if (StringUtils.isEmpty(name) || (floorPrice <= 0) || deleted == true) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }

        MarketBrand marketBrand = marketAdminBrandService.updateBrand(name, desc, picUrl, floorPrice, id, updateTime);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(marketBrand)));
    }

    private void createBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String name = JacksonUtil.parseString(requestBody, "name");
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String floorPriceParam = JacksonUtil.parseString(requestBody, "floorPrice");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(floorPriceParam)) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        Integer floorPrice = null;
        try {
            floorPrice = Integer.parseInt(floorPriceParam);
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402, "参数值不对")));
            return;
        }

        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(BigDecimal.valueOf(floorPrice));
        marketBrand.setPicUrl(picUrl == null ? "" : picUrl);
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        marketAdminBrandService.createBrand(marketBrand);
        MarketBrand data = marketAdminBrandService.getBrand(marketBrand);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }
}